#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# The script is writing simultaneously (20 jobs) on the same device
# Normally each job will have to generate many JobMedia. Let see if
# all JobMedia records are fine.
#
TestName=`basename $0`
JobName=Virtual

. scripts/functions
${rscripts}/cleanup
cp -f ${rscripts}/bacula-dir-2media-virtual.conf ${conf}/bacula-dir.conf
cp -f ${rscripts}/bacula-sd-2media-virtual.conf ${conf}/bacula-sd.conf
cp -f ${rscripts}/test-bacula-fd.conf ${conf}/bacula-fd.conf
cp -f ${rscripts}/test-console.conf ${conf}/bconsole.conf

echo "$cwd/tmp/data" >${cwd}/tmp/file-list

# To reproduce this issue, we need only one job per device and per volume
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Concurrent Jobs", "20", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Label Media", "Yes", "Device")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Label Format", "Vol-", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "20", "Director")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum File Size", "5M", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Volume Size", "32M", "Device")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Autoprune", "no", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Autoprune", "no", "Pool")'

mkdir -p $tmp/data
for i in 1 2 3 4
do
    dd if=/dev/urandom of=$tmp/data/$i.dat count=10000 bs=1024 2>/dev/null 1>/dev/null
done

start_test

mkdir $tmp/disk

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
run job=$JobName level=Full storage=vDrive-1 yes
@sleep 30
.status dir running
@sleep 30
.status dir running
@sleep 30
.status dir running
@sleep 30
.status dir running
@sleep 30
.status dir running
wait
messages
@$out $tmp/log2.out
.status dir running
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=vDrive-1

rm -f $tmp/bad-jobid.out
$bperl -e 'check_jobmedia()'
if [ -f $tmp/bad-jobid.out ]; then
    echo "Trying to restore suspicious jobids"
    while read jobid
    do
        cat <<END_OF_DATA >${tmp}/bconcmds
@$out $tmp/log3.out
@##############################
@# Information for JobId $JobId
@##############################
llist jobid=$jobid
list jobmedia jobid=$jobid
llist jobmedia jobid=$jobid
@$out $tmp/log3-$jobid.out
@##############################
@# Jobmedia list for job $jobid
@##############################
llist jobmedia jobid=$jobid
list joblog jobid=$jobid
@$out $tmp/log2.out
@#############################
restore where=$tmp/bacula-restore-$jobid
3
$jobid
m *
done
yes
wait
END_OF_DATA
        run_bconsole
        $rscripts/diff.pl -s $cwd/tmp/data -d $tmp/bacula-restore-$jobid/$cwd/tmp/data

        # get VolSessionId and VolSessionTime for the job
        vsi=`awk -F: '/Volume Session Id:/ { print $2 }' $tmp/log3-$jobid.out`
        vst=`awk -F: '/Volume Session Time:/ { print $2 }' $tmp/log3-$jobid.out`

        cd $tmp/disk
        for i in Vol-*
        do
            echo "Volume=$i" >> $tmp/$jobid.bsr
            echo "VolSessionId=$vsi" >> $tmp/$jobid.bsr
            echo "VolSessionTime=$vst" >> $tmp/$jobid.bsr
        done
        cd $cwd
        $bin/bls -v -k -b $tmp/$jobid.bsr vDrive-0 > $tmp/bls-$jobid.out
        $bperl -e "check_jobmedia_content('$tmp/log3-$jobid.out', '$tmp/bls-$jobid.out')"
    done < $tmp/bad-jobid.out
fi

stop_bacula

end_test
